/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servletuser;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sessionbean.AccountFacadeLocal;
import sessionbean.CMSClassFacadeLocal;
import sessionbean.CMSRoleFacadeLocal;
import sessionbean.StudentProfileFacadeLocal;
import supportlearning.entities.Account;
import supportlearning.entities.CMSClass;
import supportlearning.entities.CMSRole;
import supportlearning.entities.StudentProfile;

/**
 *
 * @author Huy
 */
public class createstudentprofile extends HttpServlet {

    @EJB
    private CMSRoleFacadeLocal cMSRoleFacade;
    @EJB
    private AccountFacadeLocal accountFacade;
    @EJB
    private StudentProfileFacadeLocal studentProfileFacade;
    @EJB
    private CMSClassFacadeLocal cMSClassFacade;

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String action1 = request.getParameter("action");
            if (action1 == null) {
                List<CMSClass> lisclass = cMSClassFacade.findAll();
                request.setAttribute("listclass", lisclass);
                request.getRequestDispatcher("createstudent.jsp").forward(request, response);
            } else if (action1.equals("Submit")) {
                String firstname = request.getParameter("txtfirstname");
                String lastname = request.getParameter("txtlastname");
                String username = request.getParameter("txtusername");
                String password = request.getParameter("txtpassword");
                String des = request.getParameter("desstu");
                String address = request.getParameter("txtaddress");
                String email = request.getParameter("txtemail");
                boolean gender = Boolean.parseBoolean(request.getParameter("drgender"));
                int classid = Integer.parseInt(request.getParameter("classid"));
                Date d1 = Calendar.getInstance().getTime();

                Account accobj = accountFacade.find(username);
                if (accobj == null) {
                    CMSRole roleobj = cMSRoleFacade.find(3);
                    CMSClass classobj = cMSClassFacade.find(classid);
                    Account acc = new Account(username, password, firstname, lastname, address, gender, d1, email, gender, roleobj);
                    StudentProfile stuobj = new StudentProfile(username, des, classobj);
                    acc.setStudentProfile(stuobj);
                    stuobj.setAccount(acc);
                    accountFacade.create(acc);
                    response.sendRedirect("createStudentSuccess.jsp");

                } else {
                    out.print("Trung User");
                }
            }
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
